import {
  BadRequestException,
  Body,
  Controller,
  Get,
  Param,
  Post,
  UseFilters,
  UseInterceptors,
} from '@nestjs/common';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatsService } from './cats.service';
import { Cat } from './interfaces/cat.interface';
import { HttpExceptionFilter } from 'src/common/exception/http-exception.filter';
import { ForbiddenException } from 'src/common/exception/forbidden.exception';
import { ValidationPipe } from 'src/common/pipe/validation.pipe';
import { LoggingInterceptor } from 'src/common/interceptor/logging.interceptor';
import { TransformInterceptor } from 'src/common/interceptor/transform.interceptor';

@Controller('cats')
@UseFilters(HttpExceptionFilter)
@UseInterceptors(LoggingInterceptor)
export class CatsController {
  constructor(private catsService: CatsService) {}
  @Get(':id')
  async findOne(
    @Param('id', ValidationPipe)
    id: number,
  ) {
    return this.catsService.findOne(id);
  }

  @Get()
  @UseInterceptors(TransformInterceptor)
  async findAll(): Promise<Cat[]> {
    try {
      return await this.catsService.findAll();
    } catch (error) {
      throw new BadRequestException('Something bad happened111111', {
        cause: new Error(),
        description: 'Some error description',
      });
    }
  }

  @Post()
  async create(@Body(ValidationPipe) createCatDto: CreateCatDto) {
    try {
      this.catsService.create(createCatDto);
    } catch (error) {
      throw new ForbiddenException();
    }
  }
}
